#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

import dbus
from wrt_param import WrtConst
from biz_traffic import WrtRoutingTable
from biz_privnet import WrtPrivnetManager
from biz_privnet import WrtPrivnetManagerCallback
from biz_subnetl2 import WrtSubnetl2Manager
from biz_subnetl3 import WrtSubnetl3Manager


class WrtCoreLocalNet(WrtPrivnetManagerCallback):

    def __init__(self, param, lv99_selector):
        self._param = param

        # traffic management
        self._rtTable = WrtRoutingTable()
        self._nameServer = DnsApiServer(None, WrtConst.nameServerSocketFile)

        # privnet management
        self._pnetManager = WrtPrivnetManager(self)

        # subnet management
        self._snetl2Manager = WrtSubnetl2Manager()
        self._snetl3Manager = WrtSubnetl3Manager()

        # selector
        self._selector = lv99_selector
        self._selector.set_slaves(self._rtTable, self._nameServer)

    def start(self):
        try:
            self._pnetManager.start()
            self._snetl2Manager.start()
            self._snetl3Manager.start()
            self._nameServer.start()
        except Exception:
            self.stop()
            raise

    def stop(self):
        self._snetl3Manager.stop()
        self._snetl2Manager.stop()
        self._pnetManager.stop()
        self._nameServer.stop()
        self._rtTable.dispose()

    def on_endpoint_available(self, plugin_id, endpoint_id):
        raise NotImplementedError()

    def on_endpoint_unavailable(self, plugin_id, endpoint_id, reason):
        raise NotImplementedError()

    def on_endpoint_activated(self, plugin_id, endpoint_id, endpoint_livedata):
        raise NotImplementedError()

    def on_endpoint_deactivated(self, plugin_id, endpoint_id, reason):
        raise NotImplementedError()


class DefaultSelectorLv99:

    def __init__(self):
        self._rt_table = None
        self._name_server = None
        self._ntp_list = None
        self._wins_list = None

    def dispose(self):
        self._wins_list = None
        self._ntp_list = None
        self._name_server = None
        self._rt_table = None

    def set_slaves(self, route_table, name_server, ntp_server_agent_list, wins_server_agent_list):
        assert self._rt_table is None
        self._rt_table = route_table
        self._name_server = name_server
        self._ntp_list = ntp_server_agent_list
        self._wins_list = wins_server_agent_list

    def on_lv1_connection_activated(self, connection_id, connection_livedata):
        pass

    def on_lv1_connection_deactivated(self, connection_id, reason):
        pass

    def on_lv2_connection_activated(self, connection_id, connection_livedata):
        pass

    def on_lv2_connection_deactivated(self, connection_id, reason):
        pass


class _NameServer(dbus.service.Object):

    def __init__(self):
        # start dns server
        DnsCacheServer.__init__(self)
        self.add_unix_socket_file(WrtConst.nameServerSocketFile)

        # register dbus object path
        bus_name = dbus.service.BusName('org.freedesktop.AvahiX', bus=dbus.SystemBus())
        dbus.service.Object.__init__(self, bus_name, '/')

    def dispose(self):
        self.remove_from_connection()
        super(DnsCacheServer, self).dispose()

    def add_unix_socket_file(self, filename):
        pass

    def add_ipv4_address(self, addr):
        pass

    def add_ipv6_address(self, addr):
        pass

    def add_domain(self, domain, serverList):
        pass

    def remove_domain(self, domain):
        pass
